package com.itheima.controller;

import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门管理Controller
 */
@Slf4j
@RestController
@RequestMapping("/depts")//将公共请求路径抽取到类上
public class DeptController {

    //private static Logger log= LoggerFactory.getLogger(DeptController.class);可用Slf4j注解代替

    @Autowired//Bean自动注入
    private DeptService deptService;


    /**
     * 查询部门数据
     * @return
     */
//    @GetMapping("/depts")//限定请求方式为get
    @GetMapping
    public Result list(){
        // System.out.println();输出打印日志不要用sout
        log.info("查询全部部门数据");

        //调用service查询部门数据
        List<Dept> deptList=deptService.list();
        return Result.success(deptList);
    }

    /**
     * 删除部门
     * @return
     */
//    @DeleteMapping("/depts/{id}")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        //注解PathVariable获取路径参数
        log.info("根据id删除部门：{}",id);
        //调用service删除部门
        deptService.delete(id);
        return Result.success();
    }

    /**
     * 新增部门
     * @return
     */
//    @PostMapping("/depts")
    @PostMapping
    public Result add(@RequestBody Dept dept){
        log.info("新增部门：{}" ,dept);
        //调用service新增部门
        deptService.add(dept);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result selectByID(@PathVariable Integer id){
        log.info("查询部门：{}",id);
        //调用service查询部门
        Dept dept= deptService.selectById(id);
        return Result.success(dept);
    }

    @PutMapping
    public Result updateById(@RequestBody Dept dept){
        log.info("修改部门：{}",dept);
        //调用service修改部门
        deptService.updateById(dept);
        return Result.success();
    }
}
